package io.mft.flow.core.executable.drive.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.mft.commons.function.ExecuteReturn;
import io.mft.commons.function.RequestValue;
import io.mft.commons.function.response.DsName;
import io.mft.commons.function.response.DsType;
import io.mft.commons.function.response.ResponseReader;
import io.mft.commons.function.response.ResponseWriter;
import io.mft.commons.function.response.kv.KVMap;
import io.mft.commons.function.response.kv.KVMapList;
import io.mft.commons.function.response.kv.PageObject;
import io.mft.commons.function.response.kv.PageObject.PageMetadata;
import io.mft.flow.core.executable.drive.ChainConditionDriver;
import io.mft.flow.core.executable.drive.ConditionDriveSourceProvider;
import io.mft.flow.core.executable.drive.ConditionDriverType;
import io.mft.flow.data.ChainContext;
import io.mft.flow.data.ExecutableRequest;
import io.mft.flow.data.ExecutableResponse;
/**
 * 管道流水驱动
 * @说明：TODO
 * @类型名称：PipelineChainConditionDriver
 * @创建者: 胡常建  
 * @创建时间: 2021年3月22日 下午4:55:06
 * @修改者: 胡常建  
 * @修改时间: 2021年3月22日 下午4:55:06
 */
public class PipelineChainConditionDriver extends ChainConditionDriver implements ResponseWriter, ResponseReader,ConditionDriveSourceProvider {

	Logger logger = LoggerFactory.getLogger(getClass());
	
	@Override
	public void write(String key, PageObject<KVMap> pager) {
		KVMapList kvMapList = new KVMapList(pager.getList());
		write(key, DsType.page, kvMapList);
	}
	
	@Override
	public void write(String key, DsType type, KVMapList value) {
		try {
			RequestValue requestValue=new RequestValue();
			ExecutableRequest request = new ExecutableRequest(requestValue);
			ExecutableResponse response = new ExecutableResponse();
			response.result(value);
			ChainContext chainContext = new ChainContext(chainCondition().getChain().getChainId(),request,request, response,chainCondition().getChain().getChainId());
			drive(chainContext,this);
		} catch (Exception e) {
			logger.error("", e);
		}
	}

	@Override
	public void clear() {
		// TODO Auto-generated method stub

	}

	@Override
	public void clear(String key) {
		// TODO Auto-generated method stub

	}

	@Override
	public ConditionDriverType conditionDriverType() {
		return ConditionDriverType.pipeline;
	}

	@Override
	public KVMapList list(String key) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public PageMetadata pageMetadata(String key) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<DsName> dsNames() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public DsName getDsName(String key) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ExecuteReturn drive(ChainContext context,ConditionDriveSourceProvider conditionDriveSourceProvider) throws Exception {
		Runnable runnable=new Runnable() {
			public void run() {
				try {
					IterateLoopSyncReturnChainConditionDriver iterateLoopSyncReturnChainConditionDriver=new IterateLoopSyncReturnChainConditionDriver();
					iterateLoopSyncReturnChainConditionDriver.chainCondition(chainCondition());
					iterateLoopSyncReturnChainConditionDriver.drive(context,conditionDriveSourceProvider);
				} catch (Exception e) {
					logger.error("",e);
				}
			}
		};
		runnable.run();
		//executor.submit(runnable);
		return ExecuteReturn.builder().build();
	}

	
	@Override
	public ProvideResult provide(ChainContext context) {
		ProvideResult provideResult=new ProvideResult();
		List<Object> list=context.getResponse().listObject();
		provideResult.setList(list);
		return provideResult;
	}

	@Override
	public <T> List<T> listObject(String key) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public <T> void writeObject(String key, PageObject<T> pager) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public <T> void writeObject(String key, DsType type, List<T> value) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public String mainDsKey() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void mainDsKey(String mainDsKey) {
		// TODO Auto-generated method stub
		
	}
}
